package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubContributionLogDao;
import com.game.server.db.dao.XyClubJijinLogDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYfindClubTheFundSubsidiaryReq;
import com.game.server.message.proto.out.XYclubContributionLogStruct;
import com.game.server.message.proto.out.XYclubJiJinLogStruct;
import com.game.server.message.proto.out.XYfindClubTheFundSubsidiaryRsp;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XYfindClubTheFundSubsidiaryReq extends AbstractXYfindClubTheFundSubsidiaryReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null && client.checkClick(msgcode)) {
            int limit = 20;
            XYfindClubTheFundSubsidiaryRsp rsp = new XYfindClubTheFundSubsidiaryRsp();
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
            if (clubInfo != null) {
                XyClubMemberInfo memberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (memberInfo!=null && (memberInfo.getIsManager()==1 || memberInfo.getIsManager()==2)){
                    JSONObject json = XyClubJijinLogDao.findDetailPage(limit, this.page, this.clubid, this.type);
                    rsp.setAllpage(json.getInt("allpage"));
                    rsp.setAllcount(json.getInt("allcount"));
                    rsp.setAllFoundation(json.getString("allFoundation"));
                    rsp.setPage(this.page);
                    JSONArray array = json.getJSONArray("array");
                    XYclubJiJinLogStruct[] clubjijinlogs = new XYclubJiJinLogStruct[array.size()];
                    int index = 0;
                    for (Object ent : array) {
                        JSONObject object = JSONObject.fromObject(ent);
                        XYclubJiJinLogStruct clubjijinlog = new XYclubJiJinLogStruct();
                        clubjijinlog.setOptime(object.getString("createdate"));
                        clubjijinlog.setIntegral(object.getInt("integral"));
                        clubjijinlog.setResidualFunds(object.getInt("residualFunds"));
                        int pid = object.getInt("pid");
                        int uid = object.getInt("uid");
                        int logType = object.getInt("type");
                        clubjijinlog.setType(logType);
                        clubjijinlog.setPid(pid);
                        if (logType==1 && pid!=0){
                            UserInfoData infoData = DataManager.getInstance().getMapUserData().get(pid);
                            if (infoData!=null){
                                clubjijinlog.setPhead(infoData.getHead());
                                clubjijinlog.setPname(infoData.getName());
                            }
                        }
                        clubjijinlog.setUid(uid);
                        if (logType==2){
                            UserInfoData pinfoData = DataManager.getInstance().getMapUserData().get(pid);
                            UserInfoData uinfoData = DataManager.getInstance().getMapUserData().get(uid);
                            if (pinfoData!=null){
                                clubjijinlog.setPhead(pinfoData.getHead());
                                clubjijinlog.setPname(pinfoData.getName());
                            }
                            if (uinfoData!=null){
                                clubjijinlog.setUhead(uinfoData.getHead());
                                clubjijinlog.setUname(uinfoData.getName());
                            }
                        }

                        clubjijinlogs[index] = clubjijinlog;
                        index++;
                    }
                    rsp.setClubjijinlogs(clubjijinlogs);
                }
            }
            client.Send(rsp);
        }
    }

}
